package com.example.othree.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.WheatCornGrowthYear;
import com.example.othree.entity.WheatCornGrowthYear;
import com.example.othree.entity.WheatCornGrowthYear;
import com.example.othree.service.WheatCornGrowthYearService;
import com.example.othree.service.WheatCornGrowthYearService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zogran
 * @since 2024-11-12
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class WheatCornGrowthYearController {
    @Autowired
    private WheatCornGrowthYearService wheatCornGrowthService;

    @PostMapping("/wheat/create")
    @ApiOperation("新增")
    public R create(@RequestBody WheatCornGrowthYear wheatCornGrowthYear){
        return wheatCornGrowthService.save(wheatCornGrowthYear) ? R.ok() : R.error();
    }

    @PostMapping("/wheat/saveBatch")
    @ApiOperation("批量新增")
    public R saveBatch(@RequestBody List<WheatCornGrowthYear> wheatCornGrowthYearList){
        return wheatCornGrowthService.saveBatch(wheatCornGrowthYearList) ? R.ok() : R.error();
    }

    @GetMapping("/wheat/list")
    public List<WheatCornGrowthYear> list(){
        return wheatCornGrowthService.list();
    }

    @PostMapping("/wheat/update")
    @ApiOperation("修改")
    public R update(@RequestBody WheatCornGrowthYear wheatCornGrowthYear){
        return wheatCornGrowthService.updateById(wheatCornGrowthYear) ? R.ok() : R.error();
    }

    @PostMapping("/wheat/delete/{id}")
    public R delete(@PathVariable Integer id){
        return wheatCornGrowthService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/wheat/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String 站名 = param.get("站名").toString();
        String 起始年份 = param.get("起始年份").toString();

        Page<WheatCornGrowthYear> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<WheatCornGrowthYear> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(站名) && !"null".equals(站名)){
            lambdaQueryWrapper.eq(WheatCornGrowthYear::get站名, 站名);
        }
        if(StringUtils.isNotBlank(起始年份) && !"null".equals(起始年份)){
            lambdaQueryWrapper.eq(WheatCornGrowthYear::get起始年份,起始年份);
        }

        IPage result = wheatCornGrowthService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }

}

